/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package gui;

import java.util.ArrayList;
import java.util.Map;

import javax.swing.JOptionPane;

import com.sun.org.apache.xerces.internal.impl.xpath.regex.ParseException;

import calcul.Calculator;
import calcul.DictionaryKeys;

/**
 * 
 * @author ramzes
 */
public class mainPanel extends javax.swing.JFrame {

	public void initComponentsMy() {
		this.ip.add(jTextFieldIp1);
		this.ip.add(jTextFieldIp2);
		this.ip.add(jTextFieldIp3);
		this.ip.add(jTextFieldIp4);

		this.mask.add(jTextFieldMasc1);
		this.mask.add(jTextFieldMasc2);
		this.mask.add(jTextFieldMasc3);
		this.mask.add(jTextFieldMasc4);

		jPanelShortMasc.setVisible(false);

	}

	/**
	 * Creates new form mainPanel
	 */
	public mainPanel() {
		initComponents();
		initComponentsMy();
	}

	/**
	 * This method is called from within the constructor to initialize the form.
	 * WARNING: Do NOT modify this code. The content of this method is always
	 * regenerated by the Form Editor.
	 */
	@SuppressWarnings("unchecked")
	// <editor-fold defaultstate="collapsed"
	// <editor-fold defaultstate="collapsed"
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jToggleButtonSwitch = new javax.swing.JToggleButton();
        jLabelSwImputMode = new javax.swing.JLabel();
        jLabelMode = new javax.swing.JLabel();
        jTextFieldIp1 = new javax.swing.JTextField();
        jTextFieldIp2 = new javax.swing.JTextField();
        jTextFieldIp4 = new javax.swing.JTextField();
        jTextFieldIp3 = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        jPanelShortMasc = new javax.swing.JPanel();
        jLabel4 = new javax.swing.JLabel();
        jTextFieldShortMasc = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        jPanelLongMasc = new javax.swing.JPanel();
        jLabel3 = new javax.swing.JLabel();
        jTextFieldMasc3 = new javax.swing.JTextField();
        jTextFieldMasc4 = new javax.swing.JTextField();
        jTextFieldMasc2 = new javax.swing.JTextField();
        jTextFieldMasc1 = new javax.swing.JTextField();
        jPanelOutput = new javax.swing.JPanel();
        jLabelOAddress = new javax.swing.JLabel();
        jLabelOAddres2 = new javax.swing.JLabel();
        jLabelONetMask = new javax.swing.JLabel();
        jLabelONetmask2 = new javax.swing.JLabel();
        jLabelONetwork = new javax.swing.JLabel();
        jLabelONetwork2 = new javax.swing.JLabel();
        jLabelOHostMin = new javax.swing.JLabel();
        jLabelOHostMin2 = new javax.swing.JLabel();
        jLabelOHostMax = new javax.swing.JLabel();
        jLabelOHostMax2 = new javax.swing.JLabel();
        jLabelOBroadcast = new javax.swing.JLabel();
        jLabelOBroadcast2 = new javax.swing.JLabel();
        jLabelOHostNet2 = new javax.swing.JLabel();
        jLabelOHostNet = new javax.swing.JLabel();
        jLabelOLine = new javax.swing.JLabel();
        jLabelOClass = new javax.swing.JLabel();
        jLabelOClass2 = new javax.swing.JLabel();
        jButtonOk = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jToggleButtonSwitch.setText("Switch");
        jToggleButtonSwitch.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jToggleButtonSwitchActionPerformed(evt);
            }
        });

        jLabelSwImputMode.setText("Switch input mode");

        jLabelMode.setText("Normal");

        jLabel2.setText("Ip");

        jLabel4.setText("Masca");

        jLabel1.setText("/");

        javax.swing.GroupLayout jPanelShortMascLayout = new javax.swing.GroupLayout(jPanelShortMasc);
        jPanelShortMasc.setLayout(jPanelShortMascLayout);
        jPanelShortMascLayout.setHorizontalGroup(
            jPanelShortMascLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanelShortMascLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanelShortMascLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(jPanelShortMascLayout.createSequentialGroup()
                        .addGap(16, 16, 16)
                        .addComponent(jLabel4))
                    .addGroup(jPanelShortMascLayout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jTextFieldShortMasc, javax.swing.GroupLayout.PREFERRED_SIZE, 41, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(28, Short.MAX_VALUE))
        );
        jPanelShortMascLayout.setVerticalGroup(
            jPanelShortMascLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanelShortMascLayout.createSequentialGroup()
                .addComponent(jLabel4)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanelShortMascLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jTextFieldShortMasc, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(0, 2, Short.MAX_VALUE))
        );

        jLabel3.setText("Masca");

        javax.swing.GroupLayout jPanelLongMascLayout = new javax.swing.GroupLayout(jPanelLongMasc);
        jPanelLongMasc.setLayout(jPanelLongMascLayout);
        jPanelLongMascLayout.setHorizontalGroup(
            jPanelLongMascLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanelLongMascLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanelLongMascLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanelLongMascLayout.createSequentialGroup()
                        .addComponent(jTextFieldMasc1, javax.swing.GroupLayout.PREFERRED_SIZE, 41, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jTextFieldMasc2, javax.swing.GroupLayout.PREFERRED_SIZE, 41, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jTextFieldMasc3, javax.swing.GroupLayout.PREFERRED_SIZE, 41, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jTextFieldMasc4, javax.swing.GroupLayout.PREFERRED_SIZE, 41, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanelLongMascLayout.createSequentialGroup()
                        .addGap(67, 67, 67)
                        .addComponent(jLabel3)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanelLongMascLayout.setVerticalGroup(
            jPanelLongMascLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanelLongMascLayout.createSequentialGroup()
                .addComponent(jLabel3)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanelLongMascLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jTextFieldMasc1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jTextFieldMasc2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jTextFieldMasc3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jTextFieldMasc4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(0, 8, Short.MAX_VALUE))
        );

        jLabelOAddress.setText("Address:");

        jLabelONetMask.setText("Netmask:");

        jLabelONetwork.setText("Network:");

        jLabelOHostMin.setText("HostMin:");

        jLabelOHostMax.setText("HostMax:");

        jLabelOBroadcast.setText("Broadcast:");

        jLabelOHostNet.setText("Host/Net:");

        jLabelOLine.setText("---------------------------------------------------------------");

        jLabelOClass.setText("Class:");

        javax.swing.GroupLayout jPanelOutputLayout = new javax.swing.GroupLayout(jPanelOutput);
        jPanelOutput.setLayout(jPanelOutputLayout);
        jPanelOutputLayout.setHorizontalGroup(
            jPanelOutputLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanelOutputLayout.createSequentialGroup()
                .addGroup(jPanelOutputLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanelOutputLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                        .addComponent(jLabelOLine, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addGroup(jPanelOutputLayout.createSequentialGroup()
                            .addGroup(jPanelOutputLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(jLabelOBroadcast)
                                .addComponent(jLabelONetMask)
                                .addComponent(jLabelOAddress)
                                .addComponent(jLabelOHostMax)
                                .addComponent(jLabelOHostMin)
                                .addComponent(jLabelONetwork)
                                .addComponent(jLabelOHostNet))
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addGroup(jPanelOutputLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(jLabelOHostNet2, javax.swing.GroupLayout.PREFERRED_SIZE, 300, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(jLabelOBroadcast2, javax.swing.GroupLayout.PREFERRED_SIZE, 300, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(jLabelOHostMax2, javax.swing.GroupLayout.PREFERRED_SIZE, 300, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(jLabelOHostMin2, javax.swing.GroupLayout.PREFERRED_SIZE, 300, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(jLabelONetwork2, javax.swing.GroupLayout.PREFERRED_SIZE, 300, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(jLabelONetmask2, javax.swing.GroupLayout.PREFERRED_SIZE, 300, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(jLabelOAddres2, javax.swing.GroupLayout.PREFERRED_SIZE, 300, javax.swing.GroupLayout.PREFERRED_SIZE))))
                    .addGroup(jPanelOutputLayout.createSequentialGroup()
                        .addComponent(jLabelOClass)
                        .addGap(30, 30, 30)
                        .addComponent(jLabelOClass2, javax.swing.GroupLayout.PREFERRED_SIZE, 300, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanelOutputLayout.setVerticalGroup(
            jPanelOutputLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanelOutputLayout.createSequentialGroup()
                .addGroup(jPanelOutputLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabelOAddress)
                    .addComponent(jLabelOAddres2, javax.swing.GroupLayout.PREFERRED_SIZE, 17, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanelOutputLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabelONetMask)
                    .addComponent(jLabelONetmask2, javax.swing.GroupLayout.PREFERRED_SIZE, 17, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabelOLine, javax.swing.GroupLayout.PREFERRED_SIZE, 17, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanelOutputLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabelONetwork)
                    .addComponent(jLabelONetwork2, javax.swing.GroupLayout.PREFERRED_SIZE, 17, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanelOutputLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabelOHostMin)
                    .addComponent(jLabelOHostMin2, javax.swing.GroupLayout.PREFERRED_SIZE, 17, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanelOutputLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabelOHostMax)
                    .addComponent(jLabelOHostMax2, javax.swing.GroupLayout.PREFERRED_SIZE, 17, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanelOutputLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabelOBroadcast)
                    .addComponent(jLabelOBroadcast2, javax.swing.GroupLayout.PREFERRED_SIZE, 17, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanelOutputLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabelOHostNet)
                    .addComponent(jLabelOHostNet2, javax.swing.GroupLayout.PREFERRED_SIZE, 17, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanelOutputLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabelOClass)
                    .addComponent(jLabelOClass2, javax.swing.GroupLayout.PREFERRED_SIZE, 17, javax.swing.GroupLayout.PREFERRED_SIZE)))
        );

        jButtonOk.setText("Ok");
        jButtonOk.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonOkActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(layout.createSequentialGroup()
                                        .addComponent(jLabelSwImputMode)
                                        .addGap(68, 68, 68)
                                        .addComponent(jLabel2))
                                    .addGroup(layout.createSequentialGroup()
                                        .addComponent(jToggleButtonSwitch)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(jLabelMode)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(jTextFieldIp1, javax.swing.GroupLayout.PREFERRED_SIZE, 41, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(jTextFieldIp2, javax.swing.GroupLayout.PREFERRED_SIZE, 41, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(jTextFieldIp3, javax.swing.GroupLayout.PREFERRED_SIZE, 41, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(jTextFieldIp4, javax.swing.GroupLayout.PREFERRED_SIZE, 41, javax.swing.GroupLayout.PREFERRED_SIZE)))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jPanelShortMasc, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jButtonOk, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(60, 60, 60)
                                .addComponent(jPanelLongMasc, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addGap(0, 20, Short.MAX_VALUE))
                    .addComponent(jPanelOutput, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGap(17, 17, 17)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabelSwImputMode)
                            .addComponent(jLabel2))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jToggleButtonSwitch)
                            .addComponent(jLabelMode)
                            .addComponent(jTextFieldIp1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jTextFieldIp2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jTextFieldIp3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jTextFieldIp4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addComponent(jPanelShortMasc, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanelLongMasc, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButtonOk))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanelOutput, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

	private void jToggleButtonSwitchActionPerformed(
			java.awt.event.ActionEvent evt) {
		switchPanels();
	}

	private void switchPanels() {
		if (jPanelLongMasc.isVisible()) {
			jPanelLongMasc.setVisible(false);
			jPanelShortMasc.setVisible(true);
			jLabelMode.setText("Short  ");

		} else {
			jPanelLongMasc.setVisible(true);
			jPanelShortMasc.setVisible(false);
			jLabelMode.setText("Normal");
		}

	}

	private void jButtonOkActionPerformed(java.awt.event.ActionEvent evt) {
		calculate();
	}

	private boolean checkData(ArrayList<Integer> ip, ArrayList<Integer> mask) {
		for (Integer ipValue : ip) {
			if(ipValue < 0 || ipValue > 255){
				JOptionPane.showMessageDialog(null, "Value must be between 0 and 255");
				return false;
			}
		}
		if(mask.size() ==1 ){
			if(mask.get(0) < 0 || mask.get(0) > 31){
				JOptionPane.showMessageDialog(null, "Mask value must be between 0 and 32");
				return false;
			}
		}else{
			for (Integer maskValue : mask) {
				if(maskValue < 0 || maskValue > 255){
					JOptionPane.showMessageDialog(null, "Value must be between 0 and 255");
					return false;
				}
			}
		}
		return true;
	}

	private void calculate() {
		Calculator calculator = new Calculator();
		ArrayList<Integer> ip = new ArrayList<Integer>();
		ArrayList<Integer> mask = new ArrayList<Integer>();
		if(getData(ip, mask) && checkData(ip, mask)){
			Map<DictionaryKeys, ArrayList> dictionaryRspValue = calculator
					.calculateAll(ip, mask);
			if(dictionaryRspValue != null){
				afishResults(dictionaryRspValue);
			}
			
		}
		
	}

	private void afishResults(Map dictionaryRspValue) {
		jLabelOAddres2.setText(generateStringIpFromArray(dictionaryRspValue,
				DictionaryKeys.Address, 0));
		jLabelONetmask2.setText(generateStringIpFromArray(dictionaryRspValue,
				DictionaryKeys.LongMask, 0)
				+ " = "
				+ dictionaryRspValue.get(DictionaryKeys.ShortMask));
		jLabelONetwork2.setText(generateStringIpFromArray(dictionaryRspValue,
				DictionaryKeys.BaseAdress, 0)
				+ " / "
				+ dictionaryRspValue.get(DictionaryKeys.ShortMask));
		jLabelOHostMin2.setText(generateStringIpFromArray(dictionaryRspValue,
				DictionaryKeys.BaseAdress, 1));
		jLabelOHostMax2.setText(generateStringIpFromArray(dictionaryRspValue,
				DictionaryKeys.Broadcast, -1));
		jLabelOBroadcast2.setText(generateStringIpFromArray(dictionaryRspValue,
				DictionaryKeys.Broadcast, 0));
		Object o = dictionaryRspValue.get(DictionaryKeys.Class);
		jLabelOClass2.setText(dictionaryRspValue.get(DictionaryKeys.Class)
				.toString());
		jLabelOHostNet2.setText(dictionaryRspValue.get(DictionaryKeys.HostNet).toString());
//		jLabelOHostNet2.setText("aaaa");
	}

	private String generateStringIpFromArray(
			Map<String, ArrayList> dictionaryRspValue,
			DictionaryKeys dictionaryKeys, int value) {
		String netMask = "";
		ArrayList<Integer> arr = dictionaryRspValue.get(dictionaryKeys);
		for (int i = 0, length = arr.size(); i < length; i++) {
			if (i < length - 1) {
				netMask += arr.get(i) + ".";
			} else {
				netMask += arr.get(i) + value;
			}
		}
		return netMask;
	}

	private boolean getData(ArrayList<Integer> ip, ArrayList<Integer> mask) {
		try {
			for (javax.swing.JTextField textField : this.ip) {
				ip.add(Integer.parseInt(textField.getText()));
			}
			if (jPanelLongMasc.isVisible()) {
				for (javax.swing.JTextField textField : this.mask) {
					mask.add(Integer.parseInt(textField.getText()));
				}
			} else {
				mask.add(Integer.parseInt(jTextFieldShortMasc.getText()));

			}
		} catch (Exception e) {
			JOptionPane.showMessageDialog(null, "Need to imput only numbers");
			return false;
		}
		return true;

	}

	private void jTextFieldIp1KeyTyped(javax.swing.JTextField jTextField) {
		// System.out.println(jTextField.);
	}

	/**
	 * @param args
	 *            the command line arguments
	 */
	public static void main(String args[]) {
		/* Set the Nimbus look and feel */
		// <editor-fold defaultstate="collapsed"
		// desc=" Look and feel setting code (optional) ">
		/*
		 * If Nimbus (introduced in Java SE 6) is not available, stay with the
		 * default look and feel. For details see
		 * http://download.oracle.com/javase
		 * /tutorial/uiswing/lookandfeel/plaf.html
		 */
		try {
			for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager
					.getInstalledLookAndFeels()) {
				if ("Nimbus".equals(info.getName())) {
					javax.swing.UIManager.setLookAndFeel(info.getClassName());
					break;
				}
			}
		} catch (ClassNotFoundException ex) {
			java.util.logging.Logger.getLogger(mainPanel.class.getName()).log(
					java.util.logging.Level.SEVERE, null, ex);
		} catch (InstantiationException ex) {
			java.util.logging.Logger.getLogger(mainPanel.class.getName()).log(
					java.util.logging.Level.SEVERE, null, ex);
		} catch (IllegalAccessException ex) {
			java.util.logging.Logger.getLogger(mainPanel.class.getName()).log(
					java.util.logging.Level.SEVERE, null, ex);
		} catch (javax.swing.UnsupportedLookAndFeelException ex) {
			java.util.logging.Logger.getLogger(mainPanel.class.getName()).log(
					java.util.logging.Level.SEVERE, null, ex);
		}
		// </editor-fold>

		/* Create and display the form */
		java.awt.EventQueue.invokeLater(new Runnable() {
			public void run() {
				new mainPanel().setVisible(true);
			}
		});
	}

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButtonOk;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabelMode;
    private javax.swing.JLabel jLabelOAddres2;
    private javax.swing.JLabel jLabelOAddress;
    private javax.swing.JLabel jLabelOBroadcast;
    private javax.swing.JLabel jLabelOBroadcast2;
    private javax.swing.JLabel jLabelOClass;
    private javax.swing.JLabel jLabelOClass2;
    private javax.swing.JLabel jLabelOHostMax;
    private javax.swing.JLabel jLabelOHostMax2;
    private javax.swing.JLabel jLabelOHostMin;
    private javax.swing.JLabel jLabelOHostMin2;
    private javax.swing.JLabel jLabelOHostNet;
    private javax.swing.JLabel jLabelOHostNet2;
    private javax.swing.JLabel jLabelOLine;
    private javax.swing.JLabel jLabelONetMask;
    private javax.swing.JLabel jLabelONetmask2;
    private javax.swing.JLabel jLabelONetwork;
    private javax.swing.JLabel jLabelONetwork2;
    private javax.swing.JLabel jLabelSwImputMode;
    private javax.swing.JPanel jPanelLongMasc;
    private javax.swing.JPanel jPanelOutput;
    private javax.swing.JPanel jPanelShortMasc;
    private javax.swing.JTextField jTextFieldIp1;
    private javax.swing.JTextField jTextFieldIp2;
    private javax.swing.JTextField jTextFieldIp3;
    private javax.swing.JTextField jTextFieldIp4;
    private javax.swing.JTextField jTextFieldMasc1;
    private javax.swing.JTextField jTextFieldMasc2;
    private javax.swing.JTextField jTextFieldMasc3;
    private javax.swing.JTextField jTextFieldMasc4;
    private javax.swing.JTextField jTextFieldShortMasc;
    private javax.swing.JToggleButton jToggleButtonSwitch;
    // End of variables declaration//GEN-END:variables
	private ArrayList<javax.swing.JTextField> ip = new ArrayList();
	private ArrayList<javax.swing.JTextField> mask = new ArrayList();
}
